/*
twTablePager v.1.0

- ajax load
- require jquery UI
- by tiger

sample : $("#table1").twTablePager(ajaxURL : "ListView?page=",currentPage : 3,maxPage : 10);

*/
(function($) {

	jQuery.fn.twTablePager = function(settings) {
		
		var defaults = { 
						ajaxURL : null, //sample "ListView?page="
						caption : "Page : ",
						firstTitle : "Go to first page",
						lastTitle : "Go to last page",
						prevTitle : "Go to previous page",
						nextTitle : "Go to next page",
						currentPage : 1,
						maxPage : 10,
						onPaging : function(){return true}
						};
						
		var options = jQuery.extend(defaults, settings);
		
		var holder = $(this);
		var currentPage = options.currentPage;
		
		//load ajaxURL to Holder area
		$(holder).load(options.ajaxURL+currentPage,function(){
			//set paging width
			$(this).width($(this).find("table:first").width());
			//add paging Markup
			$(this).after('<div class="paging ui-state-highlight ui-corner-bottom ui-helper-clearfix" style=" padding:2px; width:'+($(this).width()-4)+'px"><span style="float:left">'+options.caption+'</span><span class="firstPage ui-icon ui-icon-seek-first" style="cursor:pointer;float:left" title="'+options.firstTitle+'"></span><span class="prevPage ui-icon ui-icon-seek-prev" style="cursor:pointer; float:left; margin-left:5px" title="'+options.prevTitle+'"></span><div class="pageNum" style="float:left"></div><span class="nextPage ui-icon ui-icon-seek-next" style="cursor:pointer; float:left; margin-left:5px" title="'+options.nextTitle+'"></span><span class="lastPage ui-icon ui-icon-seek-end" style="cursor:pointer; float:left; margin-left:5px" title="'+options.lastTitle+'"></span></div>');
			//add page number
			for (i=1; i<=options.maxPage; i++){
				if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
				
				$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
				
				$(holder).next("div.paging").find(".pageNum span:last").click(function(){
					
					var thisNum = $(this);
					var thisPage = $(this).text();
					gotoPage(thisNum,thisPage,"pageNum");
				
				});
			}
			//end page number
			//go to first Page
			$(holder).next("div.paging").find(".firstPage").click(function(){
				var thisNum = $(this);
				var thisPage = 1;
				gotoPage(thisNum,thisPage,"firstPage");
			});
			//go to last Page
			$(holder).next("div.paging").find(".lastPage").click(function(){
				var thisNum = $(this);
				var thisPage = options.maxPage;
				gotoPage(thisNum,thisPage,"lastPage");
			});
			//go to next Page
			$(holder).next("div.paging").find(".nextPage").click(function(){
				var thisNum = $(this);
				var thisPage = Number(currentPage)+1;
				if(currentPage < options.maxPage) { gotoPage(thisNum,thisPage,"nextPage") };
			});
			//go to prev Page
			$(holder).next("div.paging").find(".prevPage").click(function(){
				var thisNum = $(this);
				var thisPage = Number(currentPage)-1;
				if(currentPage > 1) { gotoPage(thisNum,thisPage,"prevPage"); };
			});
		});

		function gotoPage(obj,pageNum,displayType){
			//ptions.onPaging(pageNum);
			if(options.onPaging(pageNum) == false ){return false}
			$(holder).html('<div style="padding:10px"><img src="'+CDNname+'images/loading.gif" alt="loading" align="absmiddle"/>Opening page please wait...</div>').load(options.ajaxURL+pageNum,		function(){
				$(holder).next("div.paging").find(".pageNum span").css({'font-weight':'normal'});
				
				switch (displayType) {
		
					case 'firstPage':
						$(holder).next("div.paging").find(".pageNum span:first").css({'font-weight':'bold'});
						break;
					
					case 'lastPage':
						$(holder).next("div.paging").find(".pageNum span:last").css({'font-weight':'bold'});
						break;
					
					case 'nextPage':
						$(holder).next("div.paging").find(".pageNum span:eq("+currentPage+")").css({'font-weight':'bold'});;
						break;
					
					case 'prevPage':
						$(holder).next("div.paging").find(".pageNum span:eq("+(currentPage-2)+")").css({'font-weight':'bold'});
						break;
					
					default:
						$(obj).css({'font-weight':'bold'});
						break;
				}
				
				currentPage = pageNum;
				
			});
		}
		
	}//end plug-in
	
	
	
})(jQuery);	